package com.itsu.job.rpc;

import com.itsu.job.config.JobProperties;
import com.itsu.job.rpc.basic.EmbedHttpServerHandler;
import com.itsu.job.rpc.basic.EmbedServer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Jerry.Su
 * @Date 2022/1/28 11:06
 */
public class JobRpcServerExecutor implements InitializingBean {

    private final JobProperties jobProperties;

    private final EmbedHttpServerHandler serverHandler;

    public JobRpcServerExecutor(JobProperties jobProperties, @Autowired(required = false) EmbedHttpServerHandler serverHandler) {
        this.jobProperties = jobProperties;
        this.serverHandler = serverHandler;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        JobProperties.Rpc rpc = jobProperties.getRpc();
        EmbedServer embedServer =
                new EmbedServer(serverHandler);
        new Thread(() -> embedServer.start(rpc.getPort())).start();
    }
}
